/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IFSv2.Forms;

import IFSv2.Connection.DBConnection;
import IFSv2.Forms.Main.MainToolBar;
import IFSv2.Forms.Main.frmMain;
import IFSv2.Forms.Utilities.frmCurrency;
import IFSv2.Forms.Utilities.frmFind;
import IFSv2.Models.mdlCurrency;
import IFSv2.Models.mdlMain;
import IFSv2.Operations.clsAPdtl;
import IFSv2.Operations.clsAPhdr;
import IFSv2.Operations.clsTab;
import IFSv2.Util.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableCellRenderer;


public class frmAPhdr extends javax.swing.JPanel {

    private final SimpleDateFormat M_DATEFORMAT = new SimpleDateFormat("MM/dd/yyyy");
    private final String M_STRPANELNAME = "frmAphdr";
    private final String M_STRFIELDCONFIG = utilSystemPath.getConfigPath()+"frmAphdrConfig.cfg";
    
    private String m_strType;
    private String m_strBookMark;
    public static String errMsg;
    private int m_intAutoNum;
    private javax.swing.table.DefaultTableModel table;
    private boolean m_blnNoError;
    private ArrayList<String> seqIDs;
     
    clsAPhdr m_clsAPhdr;
    clsAPdtl m_clsAPdtl;
   
    /*
     * Default constructor
     */
    public frmAPhdr() {
        buildPanel();
        enableAllFields(headerPanel, true);
        enableAllFields(detailPanel, true);
        m_strType = "ADD";
    }
    /*
     * This constructor will initially setup the value to the fields using the 
     * primary_key value
     */
    public frmAPhdr(String primary_key){
        buildPanel();
        m_strType = "VIEW";
        
        setValues(primary_key);
    }
    
    private void buildPanel(){
        m_intAutoNum = 1;
        seqIDs = new ArrayList();

        initComponents();
        loadComponents();
        loadCustomFields();   
    }
    
    /*
     * This will be called by the main program to update the buttons.
     * DEFAULT - Only the New button is activated
     * ADD - Save and Cancel buttons are activated
     * EDIT - Save and Cancel buttons are activated
     * VIEW - New, Edit, Delete, Print buttons are activated
     */
    public void reloadToolBar(){
        MainToolBar.enableButtons(m_strType);
    }
    
    /*
     * this will be the listener of the form from the toolbar
     */
    public void buttonClicked(final String btn){
        if(btn.isEmpty() || btn == null){
            return;
        }
        switch(btn){
            case "btnNew":
                addRecord();
                break;
            case "btnCancel":
                cancelRecord();
                break;
            case "btnEdit":
                editRecord();
                break;
            case "btnSave":
                saveRecord();
                break;
            case "btnDelete":
                deleteRecord();
                break;
            case "btnPrint":
                printRecord();
                break;
            default:
                break;
        }
    }

    
    private void loadComponents(){
        
        table = (javax.swing.table.DefaultTableModel) grdDetails.getModel();
        DefaultTableCellRenderer renderer;
        renderer = (DefaultTableCellRenderer) grdDetails.getTableHeader().getDefaultRenderer();
        renderer.setHorizontalAlignment(javax.swing.JLabel.CENTER);

        try {
            fillComponents();
        } catch (SQLException ex) {
            utilErrorHandler.printError(ex.toString(), M_STRPANELNAME, "LoadComponents");
        }        
        
        /*
         * Hide not important fields from the grid. Can be replaced.
         */
        grdDetails.removeColumn(grdDetails.getColumn("TranType"));
        grdDetails.removeColumn(grdDetails.getColumn("PostRef"));
        grdDetails.removeColumn(grdDetails.getColumn("InterCo"));
        grdDetails.removeColumn(grdDetails.getColumn("BudgetID"));
        grdDetails.removeColumn(grdDetails.getColumn("Qty"));
        grdDetails.removeColumn(grdDetails.getColumn("UnitCost"));
        grdDetails.removeColumn(grdDetails.getColumn("SubLedger"));
        grdDetails.removeColumn(grdDetails.getColumn("LCID"));
        grdDetails.removeColumn(grdDetails.getColumn("TaxID"));
        grdDetails.removeColumn(grdDetails.getColumn("Taxable"));
        grdDetails.removeColumn(grdDetails.getColumn("VAT"));
        grdDetails.removeColumn(grdDetails.getColumn("WTax"));
        grdDetails.removeColumn(grdDetails.getColumn("ProjID"));
        grdDetails.removeColumn(grdDetails.getColumn("JONo"));
        grdDetails.removeColumn(grdDetails.getColumn("SeqID"));
        grdDetails.removeColumn(grdDetails.getColumn("CurUnitCost"));
        grdDetails.removeColumn(grdDetails.getColumn("CurTaxable"));
        grdDetails.removeColumn(grdDetails.getColumn("CurVAT"));
        grdDetails.removeColumn(grdDetails.getColumn("CurWTax"));
        grdDetails.removeColumn(grdDetails.getColumn("CurTranAmt"));  
        
    }    
        
    private void loadCustomFields(){
//    
//        clsField oclsField = new clsField(M_STRFIELDCONFIG, headerPanel, false);
//        oclsField.loadConfig();
//        Map<String, String[]> map = oclsField.getHashMap();
//        //header panel lang naman yung sineset kaya yun lang yung kukuhaan ng component
//        int size = headerPanel.getComponentCount();
//        for(int i=0; i< size; i++){
//            String[] fieldDef = map.get(headerPanel.getComponent(i).getName());
//            //label para sa JLabel
//            if(headerPanel.getComponent(i) instanceof javax.swing.JLabel){
//                javax.swing.JLabel label = (javax.swing.JLabel) headerPanel.getComponent(i);
//                label.setText(fieldDef[0]);
//            }
//            //kung active, hindi or locked
//            switch(fieldDef[1]){
//                case "1":
//                    break;
//                case "0":
//                    headerPanel.getComponent(i).setVisible(false);
//                    break;
//                case "-1":
//                    headerPanel.getComponent(i).setEnabled(false);
//                    break;
//                default:
//                    break;
//            }
//            //set ng coordinates
//            headerPanel.getLayout().removeLayoutComponent(headerPanel.getComponent(i));
//            AbsoluteLayout ly = (AbsoluteLayout)headerPanel.getLayout();
//            ly.addLayoutComponent(headerPanel.getComponent(i), new org.netbeans.lib.awtextra.AbsoluteConstraints(Integer.parseInt(fieldDef[2]), Integer.parseInt(fieldDef[3]), Integer.parseInt(fieldDef[4]), Integer.parseInt(fieldDef[5])));
//            headerPanel.revalidate();
//        } 
    
    }
    
    /*
     * This function will get the due date based on the termsID and InvoiceDate
     */
    public void computeDueDate(){
        if(!(txtInvoiceDate.getText().isEmpty()) && cbTermsID.getSelectedIndex() != -1){
            try {
                String invoice = txtInvoiceDate.getText();
                int terms = termsMap.get(cbTermsID.getSelectedItem().toString());
               
                Date invoiceDate = (Date)M_DATEFORMAT.parse(invoice);
                Calendar cal = Calendar.getInstance();
                cal.setTime(invoiceDate);
                cal.add(Calendar.DATE, terms);
                txtDueDate.setText(M_DATEFORMAT.format(cal.getTime()));
            } catch (ParseException ex) {
                Logger.getLogger(frmAPhdr.class.getName()).log(Level.SEVERE, null, ex);
            }
        } 
    }
    /*
     * This function is called by the main program to allow editing/customizing
     * on this panel. 
     */
    public void setFieldSecurity(){
        JOptionPane.showMessageDialog(this, "Components are now enabled for editing. Please re-open tab\nafter editing to restore the full functionalities of the components.","Tab Settings", JOptionPane.INFORMATION_MESSAGE);
        utilField oclsField = new utilField(M_STRFIELDCONFIG, headerPanel, true);
    }

    //<editor-fold defaultstate="collapsed" desc="Query">

    private Map<Object, String> vendorTerms = new HashMap();
    private Map<String, Integer> termsMap = new HashMap();
    
    private void fillComponents() throws SQLException{
        
        /*
         * TranType
         */
        ResultSet rsTranType;

        String strTranType = "SELECT TypeDescription FROM aptrantype";
        
        rsTranType = DBConnection.getActiveConnection().createStatement().executeQuery(strTranType);
        
        while(rsTranType.next()){
            cbTranType.addItem(rsTranType.getString("TypeDescription"));
        }
        cbTranType.setSelectedIndex(0);

        rsTranType.close();

        /*
         * VendorID
         */
        ResultSet rsVendorID;

        String strVendorID = "SELECT VendorName, TermsID FROM vendor";
        
        rsVendorID = DBConnection.getActiveConnection().createStatement().executeQuery(strVendorID);
        
        while(rsVendorID.next()){
            cbVendorID.addItem(rsVendorID.getString("VendorName"));
            vendorTerms.put(rsVendorID.getString("VendorName"), rsVendorID.getString("TermsID"));
        }
        cbVendorID.setSelectedIndex(0);        
        utilAutoCompletion.enable(cbVendorID);

        rsVendorID.close();

        /*
         * TermsID
         */
        ResultSet rsTermsID;

        String strTermsID = "SELECT TermsID, DaysDue FROM paymentterms";
        
        rsTermsID = DBConnection.getActiveConnection().createStatement().executeQuery(strTermsID);
        
        while(rsTermsID.next()){
            cbTermsID.addItem(rsTermsID.getString("TermsID"));
            termsMap.put(rsTermsID.getString("TermsID"), rsTermsID.getInt("DaysDue"));
        }

        rsTermsID.close();

    }

    //</editor-fold>

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        headerPanel = new javax.swing.JPanel();
        lblDueDate = new javax.swing.JLabel();
        txtDueDate = new javax.swing.JTextField();
        txtInvoiceDate = new javax.swing.JTextField();
        txtInvoice = new javax.swing.JTextField();
        lblInvoice = new javax.swing.JLabel();
        cbTermsID = new javax.swing.JComboBox();
        lblTermsID = new javax.swing.JLabel();
        lblVoucherDate = new javax.swing.JLabel();
        lblTranType = new javax.swing.JLabel();
        cbTranType = new javax.swing.JComboBox();
        cmdTranType = new javax.swing.JButton();
        txtVoucherDate = new javax.swing.JTextField();
        txtVoucherNbr = new javax.swing.JTextField();
        cmdtxtVoucherDate = new net.sourceforge.jcalendarbutton.JCalendarButton();
        cbVendorID = new javax.swing.JComboBox();
        lblVoucherNbr = new javax.swing.JLabel();
        lblVendorID = new javax.swing.JLabel();
        cmdVendorID = new javax.swing.JButton();
        lblInvoiceDate = new javax.swing.JLabel();
        cmdtxtInvoiceDate = new org.jbundle.thin.base.screen.jcalendarbutton.JCalendarButton();
        lblDocStatus = new javax.swing.JLabel();
        txtDocStatus = new javax.swing.JTextField();
        lblOffSet = new javax.swing.JLabel();
        txtOffSet = new javax.swing.JTextField();
        detailPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        grdDetails = new javax.swing.JTable();
        btnAdd = new javax.swing.JButton();
        btnModify = new javax.swing.JButton();
        btnRemove = new javax.swing.JButton();
        totalPanel = new javax.swing.JPanel();
        lblTotalTaxable = new javax.swing.JLabel();
        lblTotalVAT = new javax.swing.JLabel();
        lblTotalTranAmt = new javax.swing.JLabel();
        totalTaxable = new javax.swing.JLabel();
        totalVAT = new javax.swing.JLabel();
        totalTranAmt = new javax.swing.JLabel();
        lblOkToPost = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setName("frmAphdr");
        setPreferredSize(new java.awt.Dimension(836, 597));

        headerPanel.setBackground(new java.awt.Color(255, 255, 255));
        headerPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Header"));
        headerPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblDueDate.setText("DueDate");
        lblDueDate.setName("lblDueDate");
        headerPanel.add(lblDueDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(468, 60, -1, -1));

        txtDueDate.setEnabled(false);
        txtDueDate.setName("txtDueDate");
        headerPanel.add(txtDueDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(535, 57, 90, -1));

        txtInvoiceDate.setEnabled(false);
        txtInvoiceDate.setName("txtInvoiceDate");
        txtInvoiceDate.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtInvoiceDateFocusLost(evt);
            }
        });
        headerPanel.add(txtInvoiceDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(319, 109, 105, -1));

        txtInvoice.setEnabled(false);
        txtInvoice.setName("txtInvoice");
        txtInvoice.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtInvoiceFocusLost(evt);
            }
        });
        headerPanel.add(txtInvoice, new org.netbeans.lib.awtextra.AbsoluteConstraints(319, 83, 105, -1));

        lblInvoice.setText("Invoice");
        lblInvoice.setName("lblInvoice");
        headerPanel.add(lblInvoice, new org.netbeans.lib.awtextra.AbsoluteConstraints(257, 86, -1, -1));

        cbTermsID.setEnabled(false);
        cbTermsID.setName("cbTermsID");
        cbTermsID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbTermsIDActionPerformed(evt);
            }
        });
        headerPanel.add(cbTermsID, new org.netbeans.lib.awtextra.AbsoluteConstraints(319, 57, 105, -1));

        lblTermsID.setText("TermsID");
        lblTermsID.setName("lblTermsID");
        headerPanel.add(lblTermsID, new org.netbeans.lib.awtextra.AbsoluteConstraints(257, 63, -1, -1));

        lblVoucherDate.setText("VoucherDate");
        lblVoucherDate.setName("lblVoucherDate");
        headerPanel.add(lblVoucherDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 109, -1, -1));

        lblTranType.setText("TranType");
        lblTranType.setName("lblTranType");
        headerPanel.add(lblTranType, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 57, -1, -1));

        cbTranType.setEnabled(false);
        cbTranType.setName("cbTranType");
        cbTranType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbTranTypeActionPerformed(evt);
            }
        });
        headerPanel.add(cbTranType, new org.netbeans.lib.awtextra.AbsoluteConstraints(92, 57, 112, -1));

        cmdTranType.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IFSv2/Images/search.png"))); // NOI18N
        cmdTranType.setEnabled(false);
        cmdTranType.setName("cmdTranType");
        cmdTranType.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cmdTranTypeMouseClicked(evt);
            }
        });
        headerPanel.add(cmdTranType, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 57, 37, -1));

        txtVoucherDate.setEnabled(false);
        txtVoucherDate.setName("txtVoucherDate");
        headerPanel.add(txtVoucherDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(92, 106, 112, -1));

        txtVoucherNbr.setEnabled(false);
        txtVoucherNbr.setName("txtVoucherNbr");
        txtVoucherNbr.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtVoucherNbrFocusLost(evt);
            }
        });
        headerPanel.add(txtVoucherNbr, new org.netbeans.lib.awtextra.AbsoluteConstraints(92, 80, 112, -1));

        cmdtxtVoucherDate.setEnabled(false);
        cmdtxtVoucherDate.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                cmdtxtVoucherDatePropertyChange(evt);
            }
        });
        cmdtxtVoucherDate.setName("cmdtxtVoucherDate");
        headerPanel.add(cmdtxtVoucherDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 106, -1, -1));

        cbVendorID.setEditable(true);
        cbVendorID.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        cbVendorID.setEnabled(false);
        cbVendorID.setName("cbVendorID");
        cbVendorID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbVendorIDActionPerformed(evt);
            }
        });
        headerPanel.add(cbVendorID, new org.netbeans.lib.awtextra.AbsoluteConstraints(92, 30, 288, -1));

        lblVoucherNbr.setText("VoucherNbr");
        lblVoucherNbr.setName("lblVoucherNbr");
        headerPanel.add(lblVoucherNbr, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 83, -1, -1));

        lblVendorID.setText("VendorID");
        lblVendorID.setName("lblVendorID");
        headerPanel.add(lblVendorID, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 33, -1, -1));

        cmdVendorID.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IFSv2/Images/search.png"))); // NOI18N
        cmdVendorID.setEnabled(false);
        cmdVendorID.setName("cmdVendorID");
        cmdVendorID.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cmdVendorIDMouseClicked(evt);
            }
        });
        headerPanel.add(cmdVendorID, new org.netbeans.lib.awtextra.AbsoluteConstraints(386, 30, 37, -1));

        lblInvoiceDate.setText("InvoiceDate");
        lblInvoiceDate.setName("lblInvoiceDate");
        headerPanel.add(lblInvoiceDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(257, 112, -1, -1));

        cmdtxtInvoiceDate.setEnabled(false);
        cmdtxtInvoiceDate.setName("cmdtxtInvoiceDate");
        cmdtxtInvoiceDate.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                cmdtxtInvoiceDatePropertyChange(evt);
            }
        });
        headerPanel.add(cmdtxtInvoiceDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 111, -1, -1));

        lblDocStatus.setText("DocStatus");
        lblDocStatus.setName("lblDocStatus");
        headerPanel.add(lblDocStatus, new org.netbeans.lib.awtextra.AbsoluteConstraints(468, 86, -1, -1));

        txtDocStatus.setEnabled(false);
        txtDocStatus.setName("txtDocStatus");
        headerPanel.add(txtDocStatus, new org.netbeans.lib.awtextra.AbsoluteConstraints(535, 83, 90, -1));

        lblOffSet.setText("OffSet");
        lblOffSet.setName("lblOffSet");
        headerPanel.add(lblOffSet, new org.netbeans.lib.awtextra.AbsoluteConstraints(469, 112, -1, -1));

        txtOffSet.setEnabled(false);
        txtOffSet.setName("txtOffSet");
        headerPanel.add(txtOffSet, new org.netbeans.lib.awtextra.AbsoluteConstraints(535, 109, 90, -1));

        detailPanel.setBackground(new java.awt.Color(255, 255, 255));
        detailPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Detail"));

        jScrollPane1.setName("");
        jScrollPane1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jScrollPane1MouseClicked(evt);
            }
        });

        grdDetails.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "No", "TranType", "PostRef", "Particulars", "Account", "AccountDescription", "SubAccount", "SubDescription", "InterCo", "BudgetID", "Qty", "CurUnitCost", "SubLedger", "LCID", "TaxID", "CurTaxable", "CurVAT", "CurWTax", "CurTranAmt", "ProjID", "JONo", "UnitCost", "Taxable", "VAT", "WTax", "TranAmt", "SeqID"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        grdDetails.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        grdDetails.setName("grdDetails");
        grdDetails.getTableHeader().setReorderingAllowed(false);
        grdDetails.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                grdDetailsKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(grdDetails);
        grdDetails.getColumnModel().getColumn(0).setMinWidth(40);
        grdDetails.getColumnModel().getColumn(0).setPreferredWidth(40);
        grdDetails.getColumnModel().getColumn(0).setMaxWidth(40);
        grdDetails.getColumnModel().getColumn(4).setPreferredWidth(100);
        grdDetails.getColumnModel().getColumn(4).setMaxWidth(100);
        grdDetails.getColumnModel().getColumn(6).setPreferredWidth(100);
        grdDetails.getColumnModel().getColumn(6).setMaxWidth(100);
        grdDetails.getColumnModel().getColumn(25).setPreferredWidth(100);
        grdDetails.getColumnModel().getColumn(25).setMaxWidth(100);

        btnAdd.setText("Add");
        btnAdd.setEnabled(false);
        btnAdd.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnAddMouseClicked(evt);
            }
        });

        btnModify.setText("Modify");
        btnModify.setEnabled(false);
        btnModify.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnModifyMouseClicked(evt);
            }
        });

        btnRemove.setText("Remove");
        btnRemove.setEnabled(false);
        btnRemove.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnRemoveMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout detailPanelLayout = new javax.swing.GroupLayout(detailPanel);
        detailPanel.setLayout(detailPanelLayout);
        detailPanelLayout.setHorizontalGroup(
            detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(detailPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnAdd)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnModify)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnRemove)
                .addContainerGap())
            .addComponent(jScrollPane1)
        );
        detailPanelLayout.setVerticalGroup(
            detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, detailPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnModify)
                    .addComponent(btnRemove))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE))
        );

        totalPanel.setBackground(new java.awt.Color(255, 255, 255));
        totalPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Total"));
        totalPanel.setMaximumSize(new java.awt.Dimension(230, 32767));
        totalPanel.setMinimumSize(new java.awt.Dimension(230, 100));
        totalPanel.setPreferredSize(new java.awt.Dimension(230, 99));

        lblTotalTaxable.setText("Taxable:");

        lblTotalVAT.setText("VAT:");

        lblTotalTranAmt.setText("TranAmt:");

        totalTaxable.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        totalTaxable.setText("0.00");
        totalTaxable.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        totalVAT.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        totalVAT.setText("0.00");
        totalVAT.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        totalTranAmt.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        totalTranAmt.setText("0.00");
        totalTranAmt.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        javax.swing.GroupLayout totalPanelLayout = new javax.swing.GroupLayout(totalPanel);
        totalPanel.setLayout(totalPanelLayout);
        totalPanelLayout.setHorizontalGroup(
            totalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(totalPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(totalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(totalPanelLayout.createSequentialGroup()
                        .addComponent(lblTotalTranAmt)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(totalTranAmt, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(totalPanelLayout.createSequentialGroup()
                        .addGroup(totalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblTotalTaxable)
                            .addComponent(lblTotalVAT))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(totalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(totalTaxable, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
                            .addComponent(totalVAT, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        totalPanelLayout.setVerticalGroup(
            totalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(totalPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(totalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTotalTaxable)
                    .addComponent(totalTaxable))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(totalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTotalVAT)
                    .addComponent(totalVAT))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(totalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(totalPanelLayout.createSequentialGroup()
                        .addComponent(totalTranAmt, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(14, 14, 14))
                    .addComponent(lblTotalTranAmt))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lblOkToPost.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        lblOkToPost.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblOkToPost.setText("Ok To Post");
        lblOkToPost.setName("lblOkToPost");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(headerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 816, Short.MAX_VALUE)
                    .addComponent(detailPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lblOkToPost, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(totalPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(headerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(detailPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(totalPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblOkToPost, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cmdtxtVoucherDatePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_cmdtxtVoucherDatePropertyChange
        if (evt.getNewValue() instanceof Date) {
            settxtVoucherDate((Date) evt.getNewValue());
        }
    }//GEN-LAST:event_cmdtxtVoucherDatePropertyChange

    private void cmdTranTypeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cmdTranTypeMouseClicked
        if (cmdTranType.isEnabled() && evt.getButton() == MouseEvent.BUTTON1)
            SearchTranType();
    }//GEN-LAST:event_cmdTranTypeMouseClicked

    private void cmdVendorIDMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cmdVendorIDMouseClicked
        if (cmdVendorID.isEnabled() && evt.getButton() == MouseEvent.BUTTON1)
            SearchVendorID();
    }//GEN-LAST:event_cmdVendorIDMouseClicked

    private void grdDetailsKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_grdDetailsKeyPressed
        
        /*
         * Deletion on Grid 
         */
        if (evt.getKeyCode() == KeyEvent.VK_DELETE) {
            if(grdDetails.getSelectedRow() != -1){
                int result = JOptionPane.showConfirmDialog(null, "Delete Record?", "", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION) {
                    table.removeRow(grdDetails.getSelectedRow());
                    computeTotals();
                }
            }  
        }
        /*
         * F6 on Grid
         */
        if (evt.getKeyCode() == KeyEvent.VK_F6){
            
            if(grdDetails.getRowCount() == 0 || grdDetails.getSelectedRow() == -1){
                openfrmAPDtl();
            }else{
                viewfrmAPDtl();
            }
        }

    }//GEN-LAST:event_grdDetailsKeyPressed

    private void btnAddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnAddMouseClicked
        if(evt.getButton() == MouseEvent.BUTTON1 && btnAdd.isEnabled()){
            openfrmAPDtl();
            btnAdd.setEnabled(false);
            btnModify.setEnabled(false);
        } 
    }//GEN-LAST:event_btnAddMouseClicked

    private void btnModifyMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnModifyMouseClicked
        if(evt.getButton() == MouseEvent.BUTTON1 && grdDetails.getSelectedRow() != -1 && btnModify.isEnabled()){
            viewfrmAPDtl();
            btnAdd.setEnabled(false);
            btnModify.setEnabled(false);
        }
    }//GEN-LAST:event_btnModifyMouseClicked

    private void btnRemoveMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnRemoveMouseClicked
       
        if (grdDetails.getSelectedRow() != -1 && evt.getButton() == MouseEvent.BUTTON1 && btnRemove.isEnabled()) {
                int result = JOptionPane.showConfirmDialog(null, "Delete Record?", "", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION) {
                    table.removeRow(grdDetails.getSelectedRow());
                    computeTotals();
                }
            }
    }//GEN-LAST:event_btnRemoveMouseClicked

    private void jScrollPane1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jScrollPane1MouseClicked
        grdDetails.requestFocus();
        grdDetails.clearSelection();
    }//GEN-LAST:event_jScrollPane1MouseClicked

    private void cbTermsIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbTermsIDActionPerformed
        computeDueDate();
    }//GEN-LAST:event_cbTermsIDActionPerformed

    private void cbVendorIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbVendorIDActionPerformed
        cbTermsID.setSelectedItem(vendorTerms.get(cbVendorID.getSelectedItem()));
    }//GEN-LAST:event_cbVendorIDActionPerformed

    private void txtInvoiceFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtInvoiceFocusLost
        checkDupInvoice(m_strType);
    }//GEN-LAST:event_txtInvoiceFocusLost

    private void cmdtxtInvoiceDatePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_cmdtxtInvoiceDatePropertyChange
        if (evt.getNewValue() instanceof Date) {
            settxtInvoiceDate((Date) evt.getNewValue());
        }
    }//GEN-LAST:event_cmdtxtInvoiceDatePropertyChange

    private void txtInvoiceDateFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtInvoiceDateFocusLost
        if("ADD".equals(m_strType) || "EDIT".equals(m_strType))
            computeDueDate();
    }//GEN-LAST:event_txtInvoiceDateFocusLost

    private void txtVoucherNbrFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtVoucherNbrFocusLost
        if("ADD".equals(m_strType) || "EDIT".equals(m_strType))
            checkDupVoucher(m_strType);
    }//GEN-LAST:event_txtVoucherNbrFocusLost

    private void cbTranTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbTranTypeActionPerformed
        if("ADD".equals(m_strType) || "EDIT".equals(m_strType))
            checkDupVoucher(m_strType);
    }//GEN-LAST:event_cbTranTypeActionPerformed

    /*
     * This function will check for any duplicate invoice. If a duplicate
     * was found, the text will be cleared
     */
    private void checkDupInvoice(String type){
    
        if(cbVendorID.getSelectedIndex() != -1 && !(txtInvoice.getText().isEmpty())){
            try {
                String _sql = "SELECT Invoice FROM aphdr WHERE Invoice = '"+txtInvoice.getText()+"' AND VendorID = '"+ utilData.getID("vendor", "VendorID", "VendorName", cbVendorID.getSelectedItem().toString()) +"'"; 
                try (ResultSet rsDupInvoice = DBConnection.getActiveConnection().createStatement().executeQuery(_sql)) {
                    if("EDIT".equals(type)){
                        rsDupInvoice.next();
                    }
                    if(rsDupInvoice.next()){
                        JOptionPane.showMessageDialog(this, "Duplicate Invoice exist on this vendor! ", "", JOptionPane.ERROR_MESSAGE);
                        txtInvoice.setText("");
                    }
                }
            } catch (SQLException ex) {}
        }
    }
    
     /*
     * This function will check for any duplicate voucher. If a duplicate
     * was found, the text will be cleared
     */
    private void checkDupVoucher(String type){
    
        if(!(txtVoucherNbr.getText().isEmpty()) && cbTranType.getSelectedIndex() != -1 && m_strType == "ADD"){
            try {
                String _sql = "SELECT VoucherNbr FROM aphdr WHERE VoucherNbr = '"+txtVoucherNbr.getText()+"' AND TranType = '"+ utilData.getID("aptrantype", "TranType", "TypeDescription", cbTranType.getSelectedItem().toString()) +"'";
                try (ResultSet rsDupVoucher = DBConnection.getActiveConnection().createStatement().executeQuery(_sql)) {
                    if("EDIT".equals(type)){
                        rsDupVoucher.next();
                    }
                    if(rsDupVoucher.next()){
                        JOptionPane.showMessageDialog(this, "Duplicate Voucher exist with this TranType! ", "", JOptionPane.ERROR_MESSAGE);
                        txtVoucherNbr.setText("");
                    }
                }
            } catch (SQLException ex) {
                Logger.getLogger(frmAPhdr.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
       
    private void computeTotals(){
    
        double totalTaxable = 0;
        double totalVAT = 0;
        double totalTranAmt = 0;
        DecimalFormat decimalFormat = new DecimalFormat("#,###,###,##0.00");
        
        for(int i=0; i< table.getRowCount(); i++){
            totalTaxable = totalTaxable + Double.parseDouble(table.getValueAt(i, table.findColumn("Taxable")).toString().replace(",","").replace("(","").replace(")",""));
            totalVAT = totalVAT + Double.parseDouble(table.getValueAt(i, table.findColumn("VAT")).toString().replace(",","").replace("(","").replace(")",""));
            totalTranAmt = totalTranAmt + Double.parseDouble(table.getValueAt(i, table.findColumn("TranAmt")).toString().replace(",","").replace("(","").replace(")",""));
        }
      
        this.totalTaxable.setText(decimalFormat.format(totalTaxable));
        this.totalVAT.setText(decimalFormat.format(totalVAT));
        this.totalTranAmt.setText(decimalFormat.format(totalTranAmt));
    }
    
    /*
     * This function will be called if the user pressed the Add button on the 
     * grid, or the user pressed the F6 button while on the blank part of the 
     * grid. This is for ADDING rows only.
     */
    private void openfrmAPDtl(){
    
        final frmAPdtl apDtl = new frmAPdtl(m_strType);
        apDtl.setVisible(true);
        apDtl.addWindowListener(new WindowListener(){

            @Override
            public void windowOpened(WindowEvent e) {}

            @Override
            public void windowClosing(WindowEvent e) {}

            @Override
            public void windowClosed(WindowEvent e) {
                btnAdd.setEnabled(true);
                btnModify.setEnabled(true);
                try{
                    Map<String, ArrayList<String>> rowData = apDtl.getDtlMap();

                    for(int i=0; i< rowData.get("No").size(); i++){
                        addNewRow(table);
                        int currRow = table.getRowCount()-1;

                        table.setValueAt(rowData.get("PostRef").get(i), currRow, table.findColumn("PostRef"));
                        table.setValueAt(rowData.get("Particulars").get(i), currRow, table.findColumn("Particulars"));
                        table.setValueAt(rowData.get("Account").get(i), currRow, table.findColumn("Account"));
                        table.setValueAt(rowData.get("AccountDescription").get(i), currRow, table.findColumn("AccountDescription"));
                        table.setValueAt(rowData.get("SubAccount").get(i), currRow, table.findColumn("SubAccount"));
                        table.setValueAt(rowData.get("SubDescription").get(i), currRow, table.findColumn("SubDescription"));
                        table.setValueAt(rowData.get("InterCo").get(i), currRow, table.findColumn("InterCo"));
                        table.setValueAt(rowData.get("BudgetID").get(i), currRow, table.findColumn("BudgetID"));
                        table.setValueAt(rowData.get("Qty").get(i), currRow, table.findColumn("Qty"));
                        table.setValueAt(rowData.get("UnitCost").get(i), currRow, table.findColumn("UnitCost"));
                        table.setValueAt(rowData.get("SubLedger").get(i), currRow, table.findColumn("SubLedger"));
                        table.setValueAt(rowData.get("LCID").get(i), currRow, table.findColumn("LCID"));
                        table.setValueAt(rowData.get("TaxID").get(i), currRow, table.findColumn("TaxID"));
                        table.setValueAt(rowData.get("Taxable").get(i), currRow, table.findColumn("Taxable"));
                        table.setValueAt(rowData.get("VAT").get(i), currRow, table.findColumn("VAT"));
                        table.setValueAt(rowData.get("WTax").get(i), currRow, table.findColumn("WTax"));
                        table.setValueAt(rowData.get("TranAmt").get(i), currRow, table.findColumn("TranAmt"));
                        table.setValueAt(rowData.get("ProjID").get(i), currRow, table.findColumn("ProjID"));
                        table.setValueAt(rowData.get("JONo").get(i), currRow, table.findColumn("JONo")); 
                        table.setValueAt(rowData.get("CurUnitCost").get(i), currRow, table.findColumn("CurUnitCost"));
                        table.setValueAt(rowData.get("CurTaxable").get(i), currRow, table.findColumn("CurTaxable"));
                        table.setValueAt(rowData.get("CurVAT").get(i), currRow, table.findColumn("CurVAT"));
                        table.setValueAt(rowData.get("CurWTax").get(i), currRow, table.findColumn("CurWTax"));
                        table.setValueAt(rowData.get("CurTranAmt").get(i), currRow, table.findColumn("CurTranAmt"));
                        table.setValueAt(0, currRow, table.findColumn("SeqID"));

                        computeTotals();
                    }
                }catch(Exception ex){}
            }

            @Override
            public void windowIconified(WindowEvent e) {}
            @Override
            public void windowDeiconified(WindowEvent e) {}
            @Override
            public void windowActivated(WindowEvent e) {}
            @Override
            public void windowDeactivated(WindowEvent e) {}
        });
    }
    
    /*
     * This function will be called if the user pressed the Edit button on the 
     * grid, or the user pressed the F6 button while there is a current row 
     * selected on the grid. This is for VIEWING or EDITING rows only.
     */
    private void viewfrmAPDtl(){
        
        int currRow = grdDetails.getSelectedRow();
        java.util.HashMap rowMap = new java.util.HashMap();
        for(int i=1; i< table.getColumnCount();i++){
            
            rowMap.put(table.getColumnName(i), table.getValueAt(currRow, i));
            
          }
        final frmAPdtl apDtl = new frmAPdtl(rowMap, m_strType);
        apDtl.setVisible(true);
        apDtl.addWindowListener(new WindowListener(){

            @Override
            public void windowOpened(WindowEvent e) {}

            @Override
            public void windowClosing(WindowEvent e) {}

            @Override
            public void windowClosed(WindowEvent e) {
                btnAdd.setEnabled(true);
                btnModify.setEnabled(true);
                if("ADD".equals(m_strType) || "EDIT".equals(m_strType)){
                    Map<String, ArrayList<String>> rowData = apDtl.getDtlMap();
                    int currRow = grdDetails.getSelectedRow();
                    for(int i=0; i< rowData.get("No").size(); i++){

                        table.setValueAt(rowData.get("PostRef").get(i), currRow, table.findColumn("PostRef"));
                        table.setValueAt(rowData.get("Particulars").get(i), currRow, table.findColumn("Particulars"));
                        table.setValueAt(rowData.get("Account").get(i), currRow, table.findColumn("Account"));
                        table.setValueAt(rowData.get("AccountDescription").get(i), currRow, table.findColumn("AccountDescription"));
                        table.setValueAt(rowData.get("SubAccount").get(i), currRow, table.findColumn("SubAccount"));
                        table.setValueAt(rowData.get("SubDescription").get(i), currRow, table.findColumn("SubDescription"));
                        table.setValueAt(rowData.get("InterCo").get(i), currRow, table.findColumn("InterCo"));
                        table.setValueAt(rowData.get("BudgetID").get(i), currRow, table.findColumn("BudgetID"));
                        table.setValueAt(rowData.get("Qty").get(i), currRow, table.findColumn("Qty"));
                        table.setValueAt(rowData.get("UnitCost").get(i), currRow, table.findColumn("UnitCost"));
                        table.setValueAt(rowData.get("SubLedger").get(i), currRow, table.findColumn("SubLedger"));
                        table.setValueAt(rowData.get("LCID").get(i), currRow, table.findColumn("LCID"));
                        table.setValueAt(rowData.get("TaxID").get(i), currRow, table.findColumn("TaxID"));
                        table.setValueAt(rowData.get("Taxable").get(i), currRow, table.findColumn("Taxable"));
                        table.setValueAt(rowData.get("VAT").get(i), currRow, table.findColumn("VAT"));
                        table.setValueAt(rowData.get("WTax").get(i), currRow, table.findColumn("WTax"));
                        table.setValueAt(rowData.get("TranAmt").get(i), currRow, table.findColumn("TranAmt"));
                        table.setValueAt(rowData.get("ProjID").get(i), currRow, table.findColumn("ProjID"));
                        table.setValueAt(rowData.get("JONo").get(i), currRow, table.findColumn("JONo"));
                        table.setValueAt(rowData.get("CurUnitCost").get(i), currRow, table.findColumn("CurUnitCost"));
                        table.setValueAt(rowData.get("CurTaxable").get(i), currRow, table.findColumn("CurTaxable"));
                        table.setValueAt(rowData.get("CurVAT").get(i), currRow, table.findColumn("CurVAT"));
                        table.setValueAt(rowData.get("CurWTax").get(i), currRow, table.findColumn("CurWTax"));
                        table.setValueAt(rowData.get("CurTranAmt").get(i), currRow, table.findColumn("CurTranAmt"));
                        table.setValueAt(0, currRow, table.findColumn("SeqID"));
                        System.out.print("val:" + table.getValueAt(currRow, table.findColumn("SeqID")) + "\n");
                        
                    }
                    computeTotals();
                }                            
            }

            @Override
            public void windowIconified(WindowEvent e) {}
            @Override
            public void windowDeiconified(WindowEvent e) {}
            @Override
            public void windowActivated(WindowEvent e) {}
            @Override
            public void windowDeactivated(WindowEvent e) {}
        });
    
    }
    
    private void SearchTranType(){
    
        final frmFind fFindGrid;
        String strText = ((javax.swing.JTextField) cbTranType.getEditor().getEditorComponent()).getText();
        
        if(strText.isEmpty())
            fFindGrid = new frmFind(null, true, "aptrantype","TypeDescription", "TranType");
        else 
            fFindGrid = new frmFind(null, true, "aptrantype","TypeDescription",cbTranType.getSelectedItem().toString(), "TranType");
         
        fFindGrid.setVisible(true);
        fFindGrid.addWindowListener(new WindowListener(){
            //<editor-fold defaultstate="collapsed" desc="abstract methods">
            @Override
            public void windowOpened(WindowEvent e) {}

            @Override
            public void windowClosing(WindowEvent e) {}

            @Override
            public void windowClosed(WindowEvent e) {
                if(!"".equals(fFindGrid.getSelectedID()))
                 cbTranType.setSelectedItem(fFindGrid.getSelectedID());
            }
            
            @Override
            public void windowIconified(WindowEvent e) {}

            @Override
            public void windowDeiconified(WindowEvent e) {}

            @Override
            public void windowActivated(WindowEvent e) {}

            @Override
            public void windowDeactivated(WindowEvent e) {}
            //</editor-fold>
        });

    }

    private void SearchVendorID(){
    
        final frmFind fFindGrid;
        String strText = ((javax.swing.JTextField) cbVendorID.getEditor().getEditorComponent()).getText();
        
        if(strText.isEmpty())
            fFindGrid = new frmFind(null, true, "vendor","VendorName", "VendorID");
        else 
            fFindGrid = new frmFind(null, true, "vendor","VendorName",cbVendorID.getSelectedItem().toString(), "VendorID");
        
        fFindGrid.setVisible(true);
        fFindGrid.addWindowListener(new WindowListener(){
            //<editor-fold defaultstate="collapsed" desc="abstract methods">
            @Override
            public void windowOpened(WindowEvent e) {}

            @Override
            public void windowClosing(WindowEvent e) {}

            @Override
            public void windowClosed(WindowEvent e) {
                if(!"".equals(fFindGrid.getSelectedID()))
                 cbVendorID.setSelectedItem(fFindGrid.getSelectedID());
            }
            
            @Override
            public void windowIconified(WindowEvent e) {}

            @Override
            public void windowDeiconified(WindowEvent e) {}

            @Override
            public void windowActivated(WindowEvent e) {}

            @Override
            public void windowDeactivated(WindowEvent e) {}
            //</editor-fold>
        });

    }

    public void settxtVoucherDate(Date datDate){
        String strDate  = "";

        if (datDate != null)
            strDate = M_DATEFORMAT.format(datDate);
        
        txtVoucherDate.setText(strDate);
        cmdtxtVoucherDate.setTargetDate(datDate);
    }

    public void settxtInvoiceDate(Date datDate){
        String strDate  = "";

        if (datDate != null)
            strDate = M_DATEFORMAT.format(datDate);
        
        txtInvoiceDate.setText(strDate);
        txtInvoiceDate.requestFocus();
        txtInvoiceDate.transferFocus();
    }

    /*
     * Enables/Disables all the components inside the specified panel.
     */
    private void enableAllFields(javax.swing.JPanel panel, boolean blnOption){
        
        for (int intIndex = 0; intIndex < panel.getComponentCount(); intIndex++) {
            
            if(panel.getComponent(intIndex) instanceof javax.swing.JPanel){
                javax.swing.JPanel thisPanel = (javax.swing.JPanel) panel.getComponent(intIndex);
                enableAllFields(thisPanel, blnOption);
            }else if (panel.getComponent(intIndex) instanceof javax.swing.JTextField) {
                javax.swing.JTextField txtField = (javax.swing.JTextField) panel.getComponent(intIndex);
                txtField.setEnabled(blnOption);
            } else if (panel.getComponent(intIndex) instanceof javax.swing.JComboBox) {
                javax.swing.JComboBox cbField = (javax.swing.JComboBox) panel.getComponent(intIndex);
                cbField.setEnabled(blnOption);
            } else if (panel.getComponent(intIndex) instanceof javax.swing.JCheckBox) {
                javax.swing.JCheckBox chkField = (javax.swing.JCheckBox) panel.getComponent(intIndex);
                chkField.setEnabled(blnOption);
            } else if (panel.getComponent(intIndex) instanceof javax.swing.JTextArea) {
                javax.swing.JTextArea txtField = (javax.swing.JTextArea) panel.getComponent(intIndex);
                txtField.setEnabled(blnOption);
            } else if (panel.getComponent(intIndex) instanceof net.sourceforge.jcalendarbutton.JCalendarButton) {
                net.sourceforge.jcalendarbutton.JCalendarButton calField = (net.sourceforge.jcalendarbutton.JCalendarButton) panel.getComponent(intIndex);
                calField.setEnabled(blnOption);
            } else if (panel.getComponent(intIndex) instanceof javax.swing.JButton){
                javax.swing.JButton btnField = (javax.swing.JButton) panel.getComponent(intIndex);
                btnField.setEnabled(blnOption);
            } 
            
            /*
             * table
             */
            grdDetails.clearSelection();
        }
    }

    /*
     * Clears all the components inside the specified panel.
     */
    private void clearAllFields(javax.swing.JPanel panel) {

        for (int intIndex = 0; intIndex < panel.getComponentCount(); intIndex++) {
            
            if(panel.getComponent(intIndex) instanceof javax.swing.JPanel){
                javax.swing.JPanel thisPanel = (javax.swing.JPanel) panel.getComponent(intIndex);
                clearAllFields(thisPanel);
            } else if (panel.getComponent(intIndex) instanceof javax.swing.JTextField) {
                javax.swing.JTextField txtField = (javax.swing.JTextField) panel.getComponent(intIndex);
                txtField.setText("");
            } else if (panel.getComponent(intIndex) instanceof javax.swing.JComboBox) {
                javax.swing.JComboBox cbField = (javax.swing.JComboBox) panel.getComponent(intIndex);
                if (cbField.getItemCount() == 0) {
                } else {
                    cbField.setSelectedIndex(-1);
                }
            } else if (panel.getComponent(intIndex) instanceof javax.swing.JCheckBox) {
                javax.swing.JCheckBox chkField = (javax.swing.JCheckBox) panel.getComponent(intIndex);
                chkField.setSelected(false);
            } else if (panel.getComponent(intIndex) instanceof javax.swing.JTextArea) {
                javax.swing.JTextArea txtField = (javax.swing.JTextArea) panel.getComponent(intIndex);
                txtField.setText("");
            } 
        }

        table.setRowCount(0);
    }
    /*
     * This function is used to update the current currency of the transaction
     */
    private void setCurrency(){
        frmCurrency currency = new frmCurrency(null, true);
        currency.setVisible(true);
    }
    
    public void addRecord() {        
        m_strType = "ADD";
        m_intAutoNum = 1;
        
        clearAllFields(this);
        enableAllFields(this, true);
        txtVoucherDate.setText(M_DATEFORMAT.format(new Date()));
        
        totalTaxable.setText("0.00");
        totalVAT.setText("0.00");
        totalTranAmt.setText("0.00");
        
        mdlMain.tabMap.remove(((clsTab)frmMain.menuTabbedPane.getTabComponentAt(frmMain.menuTabbedPane.getSelectedIndex())).getTitle());
        mdlMain.tabMap.put("Voucher Entry", this);
        ((clsTab)frmMain.menuTabbedPane.getTabComponentAt(frmMain.menuTabbedPane.getSelectedIndex())).setTitle("Voucher Entry");
        
        reloadToolBar();
    }

    public void addNewRow(javax.swing.table.DefaultTableModel table){
        table.addRow(new Object[]{
                m_intAutoNum++
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
                , ""
        });
    }
  

    public void editRecord() {
        setValues(m_strBookMark);
        enableAllFields(this, true);

        txtVoucherNbr.requestFocus();
        m_strType = "EDIT";
        
        reloadToolBar();
    }

    public void deleteRecord(){
        int result = JOptionPane.showConfirmDialog(null, "Delete Entry?", "Delete Record", JOptionPane.YES_NO_OPTION);
        if(result != JOptionPane.OK_OPTION){
            return;
        }
        
        if(m_clsAPdtl.deleteRecord() && m_clsAPhdr.deleteRow()){
            m_strType = "DEFAULT";
            m_clsAPhdr.clearFieldProperties();
            m_clsAPdtl.clearFieldProperties();
            try {
                DBConnection.getActiveConnection().commit();
            } catch (SQLException ex) {
                utilErrorHandler.printError("Changes were not saved!", "frmAPhdr", "deleteRecord");
            }
            
            JOptionPane.showMessageDialog(this, "Record Deleted!", "Delete Record", JOptionPane.OK_OPTION);
            mdlMain.tabMap.remove(((clsTab)frmMain.menuTabbedPane.getTabComponentAt(frmMain.menuTabbedPane.getSelectedIndex())).getTitle());
            frmMain.menuTabbedPane.remove(this);
            
        }else{
            utilErrorHandler.printError("Unable to delete record!", "frmAPhdr", "deleteRecord");
        }
        
        clearAllFields(this);
        reloadToolBar();  
    }

    private void cancelRecord(){
        
        switch (m_strType) {
            case "EDIT":
                setValues(m_strBookMark);
                m_strType = "VIEW";
                break;
            case "ADD":
                clearAllFields(this);
                m_strType = "DEFAULT";
                break;
        }
        
        enableAllFields(this, false);
        reloadToolBar();
    }
    
    public void saveRecord(){
        m_blnNoError = true;
        m_clsAPhdr = new clsAPhdr();
        String strPrimaryKey = m_strBookMark;
        String strPrimaryKey1 = txtVoucherNbr.getText();
        String strType = "ADD";
        
        if("ADD".equals(m_strType)) {
            strPrimaryKey =  utilData.GetNextVal(m_clsAPhdr.getTableName(), "BatNbr") + "";
            m_strBookMark = strPrimaryKey;
        }
        
        m_clsAPhdr.setBatNbr(strPrimaryKey);
        m_clsAPhdr.setVoucherNbr(strPrimaryKey1);
        m_clsAPhdr.setVoucherDate(txtVoucherDate.getText());
        m_clsAPhdr.setTranType(utilData.getID("aptrantype", "TranType", "TypeDescription", cbTranType.getSelectedItem()));
        m_clsAPhdr.setVendorID(utilData.getID("vendor", "VendorID", "VendorName", cbVendorID.getSelectedItem()));
        m_clsAPhdr.setSubLedger(utilData.getID("vendor", "VendorID", "VendorName", cbVendorID.getSelectedItem()));
        m_clsAPhdr.setInvoice(txtInvoice.getText());
        m_clsAPhdr.setInvoiceDate(txtInvoiceDate.getText());
        m_clsAPhdr.setTermsID(cbTermsID.getSelectedItem() + "");
        m_clsAPhdr.setDueDate(txtDueDate.getText());
        m_clsAPhdr.setTaxable(totalTaxable.getText());
        m_clsAPhdr.setVAT(totalVAT.getText());
        m_clsAPhdr.setOffSet(txtOffSet.getText());
        m_clsAPhdr.setAmount(totalTranAmt.getText());
        if (!m_clsAPhdr.saveNewRow(m_strType)) {
            utilErrorHandler.printError(errMsg, M_STRPANELNAME, "SaveNewRow");
            errMsg = "";
            m_blnNoError = false;
            return;
        }

        /*
         * For the detail part, the program will first get all the seqIDs from
         * the grid. The system will then check if there are records that were
         * edited (the seqIDs will be set to zero(0) if the record was edited).
         * The program will delete all the records that are not on the list of
         * seqIDs then add the all the remaining records (those records with 
         * zero(0) seqIDs). It will not do anything on the unedited record.
         */
        
        seqIDs.clear();
        
        for (int intIndex = 0; intIndex < table.getRowCount(); intIndex++) {    
            if (!(table.getValueAt(intIndex, table.findColumn("SeqID")) == null))
                seqIDs.add(table.getValueAt(intIndex, table.findColumn("SeqID")).toString());
        }
        
        boolean chk = true;
                
        switch (m_strType) {
            case "ADD":
                chk = true;
                
                for(int intIndex=0; intIndex<table.getRowCount(); intIndex++){
                
                    if(chk){
                        chk = saveRow(intIndex, m_strType, strPrimaryKey, strPrimaryKey1);
                    }
                    else{
                        m_blnNoError = false;
                        try {
                            DBConnection.getActiveConnection().rollback();
                        } catch (SQLException ex) {
                            Logger.getLogger(frmAPhdr.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        break;
                    }
                }
                break;
                
            case "EDIT":
                
                if (!seqIDs.isEmpty()) {
                    String seqList = "";
                    
                    for (int intIndex=0; intIndex < seqIDs.size() ; intIndex++){
                        if("0".equals(seqIDs.get(intIndex))){
                            continue;
                        }
                        if("".equals(seqList))
                            seqList = seqIDs.get(intIndex);
                        else
                            seqList = seqList + ", " + seqIDs.get(intIndex);
                    }
                    
                    if(!("".equals(seqList))){
                        String _sql = "DELETE FROM apdtl WHERE BatNbr = " + utilData.FormatData(m_clsAPhdr.getBatNbr(), m_clsAPhdr.getDataTypeName("BatNbr"))
                                + " AND SeqID NOT IN ( " + seqList + " )";

                        try {
                            DBConnection.getActiveConnection().createStatement().executeUpdate(_sql);
                        } catch (SQLException ex) {
                            utilErrorHandler.printError(ex.toString(), M_STRPANELNAME, "Edit");
                            m_blnNoError = false;
                            return;
                        }
                    }
                }

                for(int intIndex=0; intIndex < table.getRowCount(); intIndex++){
                
                    if (chk){
                        if(table.getValueAt(intIndex, table.findColumn("SeqID")) == 0){
                            chk = saveRow(intIndex, strType, strPrimaryKey, strPrimaryKey1);
                        }
                    }else {
                        m_blnNoError = false;
                        break;
                    }
                }
                break;
        }
        
        if(m_blnNoError){
            try {
                DBConnection.getActiveConnection().commit();
            } catch (SQLException ex) {
                Logger.getLogger(frmAPhdr.class.getName()).log(Level.SEVERE, null, ex);
            }
            this.m_strType = "SAVE";
            mdlMain.tabMap.remove(((clsTab)frmMain.menuTabbedPane.getTabComponentAt(frmMain.menuTabbedPane.getSelectedIndex())).getTitle());
            mdlMain.tabMap.put("Voucher:"+strPrimaryKey1, this);
            ((clsTab)frmMain.menuTabbedPane.getTabComponentAt(frmMain.menuTabbedPane.getSelectedIndex())).setTitle("Voucher:"+strPrimaryKey1);
            
            setValues(strPrimaryKey);
            enableAllFields(this, false);
            reloadToolBar();
        }
    }

    private boolean saveRow(int rowNumber, String strType, String strPrimaryKey, String strPrimaryKey1){
        
        m_clsAPdtl = new clsAPdtl();
        m_clsAPdtl.setBatNbr(strPrimaryKey);
        m_clsAPdtl.setVoucherNbr(strPrimaryKey1);
        m_clsAPdtl.setTranType(utilData.getID("aptrantype", "TranType", "TypeDescription", cbTranType.getSelectedItem()));
        m_clsAPdtl.setPostRef(table.getValueAt(rowNumber, table.findColumn("PostRef"))+ "");
        m_clsAPdtl.setParticulars(table.getValueAt(rowNumber, table.findColumn("Particulars"))+ "");
        m_clsAPdtl.setAccount(table.getValueAt(rowNumber, table.findColumn("Account"))+ "");
        m_clsAPdtl.setSubAccount(table.getValueAt(rowNumber, table.findColumn("SubAccount"))+ "");
        m_clsAPdtl.setInterCo(table.getValueAt(rowNumber, table.findColumn("InterCo"))+ "");
        m_clsAPdtl.setBudgetID(table.getValueAt(rowNumber, table.findColumn("BudgetID"))+ "");
        m_clsAPdtl.setQty(table.getValueAt(rowNumber, table.findColumn("Qty"))+ "");
        m_clsAPdtl.setSubLedger(table.getValueAt(rowNumber, table.findColumn("SubLedger"))+ "");
        m_clsAPdtl.setLCID(table.getValueAt(rowNumber, table.findColumn("LCID"))+ "");
        m_clsAPdtl.setTaxID(table.getValueAt(rowNumber, table.findColumn("TaxID"))+ "");
        
        m_clsAPdtl.setUnitCost(getBaseRate(table.getValueAt(rowNumber, table.findColumn("CurUnitCost"))+ ""));
        m_clsAPdtl.setTaxable(getBaseRate(table.getValueAt(rowNumber, table.findColumn("CurTaxable"))+ ""));
        m_clsAPdtl.setVAT(getBaseRate(table.getValueAt(rowNumber, table.findColumn("CurVAT"))+ ""));
        m_clsAPdtl.setWTax(getBaseRate(table.getValueAt(rowNumber, table.findColumn("CurWTax"))+ ""));
        m_clsAPdtl.setTranAmt(getBaseRate(table.getValueAt(rowNumber, table.findColumn("CurTranAmt"))+ ""));
        
        m_clsAPdtl.setCurUnitCost(table.getValueAt(rowNumber, table.findColumn("CurUnitCost"))+ "");
        m_clsAPdtl.setCurTaxable(table.getValueAt(rowNumber, table.findColumn("CurTaxable"))+ "");
        m_clsAPdtl.setCurVAT(table.getValueAt(rowNumber, table.findColumn("CurVAT"))+ "");
        m_clsAPdtl.setCurWTax(table.getValueAt(rowNumber, table.findColumn("CurWTax"))+ "");
        m_clsAPdtl.setCurTranAmt(table.getValueAt(rowNumber, table.findColumn("CurTranAmt"))+ "");
        
        m_clsAPdtl.setProjID(table.getValueAt(rowNumber, table.findColumn("ProjID"))+ "");
        m_clsAPdtl.setJONo(table.getValueAt(rowNumber, table.findColumn("JONo"))+ "");
        m_clsAPdtl.setLineSeqID(table.getValueAt(rowNumber, table.findColumn("No"))+ "");
        m_clsAPdtl.setSeqID(table.getValueAt(rowNumber, table.findColumn("SeqID"))+ "");

        if (!m_clsAPdtl.saveNewRow(strType)) {
            m_blnNoError = false;
            return false;
        }
        
        m_blnNoError = true;
        return true;
    }
    
    /*
     * Converts the value of the amount to the default currency
     */
    private String getBaseRate(String currRate){
        if(currRate.length() == 0){
            return null;
        }
        currRate = currRate.replace(",","").replace("(","").replace(")","");
        double rate = Double.parseDouble(currRate) / mdlCurrency.exchangeRate;
        rate = (double)(Math.round(rate*100))/100;
        return String.valueOf(rate);
    }
    
    public void printRecord(){
        /*
         * Code not included
         */
    }
    
    private void setValues(String strCurrentID){
        clearAllFields(this);
        
        /*
         * Header part
         */
        m_clsAPhdr = new clsAPhdr();
        
        if (m_clsAPhdr.getCurrentRow(strCurrentID)) {
            m_strBookMark = m_clsAPhdr.getBatNbr();
            txtVoucherNbr.setText(m_clsAPhdr.getVoucherNbr());
            txtVoucherDate.setText(utilData.formatDate(m_clsAPhdr.getVoucherDate()));
            cbTranType.setSelectedItem(utilData.getDescription("aptrantype", "TranType", "TypeDescription", m_clsAPhdr.getTranType()));
            cbVendorID.setSelectedItem(utilData.getDescription("vendor", "VendorID", "VendorName", m_clsAPhdr.getVendorID()));
            txtInvoice.setText(m_clsAPhdr.getInvoice());
            txtInvoiceDate.setText(utilData.formatDate(m_clsAPhdr.getInvoiceDate()));
            cbTermsID.setSelectedItem(m_clsAPhdr.getTermsID());
            txtDueDate.setText(utilData.formatDate(m_clsAPhdr.getDueDate()));
            totalTaxable.setText(m_clsAPhdr.getTaxable());
            totalVAT.setText(m_clsAPhdr.getVAT());
            totalTranAmt.setText(m_clsAPhdr.getAmount()); 
            txtOffSet.setText(m_clsAPhdr.getOffSet());      
        }

        /*
         * Detail part
         */
        m_clsAPdtl = new clsAPdtl();
        
        String strPrimaryKeyName = "BatNbr";
        String strSQL = "SELECT * " +
                "FROM apdtl "+
                "WHERE " + strPrimaryKeyName + " = ";
        
        if (m_clsAPdtl.getCurrentData(strSQL, strCurrentID, strPrimaryKeyName)) {
            ResultSet rsDetail = m_clsAPdtl.getDetail();
            
            try {
                
                while(rsDetail.next()){
                    Object[] data = new Object[table.getColumnCount()];
                    data[0] = rsDetail.getRow();
                    data[table.findColumn("TranType")] = rsDetail.getString("TranType");
                    data[table.findColumn("PostRef")] = rsDetail.getString("PostRef");
                    data[table.findColumn("Particulars")] = rsDetail.getString("Particulars");
                    data[table.findColumn("Account")] = rsDetail.getString("Account");
                    data[table.findColumn("AccountDescription")] = utilData.getDescription("account", "Account", "AccountDesc", rsDetail.getString("Account"));
                    data[table.findColumn("SubAccount")] = rsDetail.getString("SubAccount");
                    data[table.findColumn("SubDescription")] = utilData.getDescription("subaccount", "SubAcct", "SubDesc", rsDetail.getString("SubAccount"));
                    data[table.findColumn("InterCo")] = rsDetail.getString("InterCo");
                    data[table.findColumn("BudgetID")] = rsDetail.getString("BudgetID");
                    data[table.findColumn("Qty")] = rsDetail.getString("Qty");
                    data[table.findColumn("UnitCost")] = rsDetail.getString("UnitCost");
                    data[table.findColumn("SubLedger")] = rsDetail.getObject("SubLedger");
                    data[table.findColumn("LCID")] = rsDetail.getObject("LCID");
                    data[table.findColumn("TaxID")] = rsDetail.getString("TaxID");
                    data[table.findColumn("Taxable")] = rsDetail.getString("Taxable");
                    data[table.findColumn("VAT")] = rsDetail.getString("VAT");
                    data[table.findColumn("WTax")] = rsDetail.getString("WTax");
                    data[table.findColumn("TranAmt")] = rsDetail.getObject("TranAmt");
                    data[table.findColumn("CurUnitCost")] = rsDetail.getString("CurUnitCost");
                    data[table.findColumn("CurTaxable")] = rsDetail.getString("CurTaxable");
                    data[table.findColumn("CurVAT")] = rsDetail.getString("CurVAT");
                    data[table.findColumn("CurWTax")] = rsDetail.getString("CurWTax");
                    data[table.findColumn("CurTranAmt")] = rsDetail.getObject("CurTranAmt");
                    data[table.findColumn("ProjID")] = rsDetail.getObject("ProjID");
                    data[table.findColumn("JONo")] = rsDetail.getObject("JONo");
                    data[table.findColumn("SeqID")] = rsDetail.getObject("SeqID");
                    table.addRow(data);
                }
                
                m_intAutoNum = Integer.parseInt((table.getRowCount() == 0)? "0" : table.getValueAt(table.getRowCount()-1, 0).toString())+1;
                table.fireTableDataChanged();
                computeTotals();
            } catch (SQLException ex) {
                utilErrorHandler.printError("Unable to retrieve data from table", "frmAPhdr", "SetValues");
            }
        }
        
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnModify;
    private javax.swing.JButton btnRemove;
    private javax.swing.JComboBox cbTermsID;
    private javax.swing.JComboBox cbTranType;
    private javax.swing.JComboBox cbVendorID;
    private javax.swing.JButton cmdTranType;
    private javax.swing.JButton cmdVendorID;
    private org.jbundle.thin.base.screen.jcalendarbutton.JCalendarButton cmdtxtInvoiceDate;
    private net.sourceforge.jcalendarbutton.JCalendarButton cmdtxtVoucherDate;
    private javax.swing.JPanel detailPanel;
    private javax.swing.JTable grdDetails;
    private javax.swing.JPanel headerPanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblDocStatus;
    private javax.swing.JLabel lblDueDate;
    private javax.swing.JLabel lblInvoice;
    private javax.swing.JLabel lblInvoiceDate;
    private javax.swing.JLabel lblOffSet;
    private javax.swing.JLabel lblOkToPost;
    private javax.swing.JLabel lblTermsID;
    private javax.swing.JLabel lblTotalTaxable;
    private javax.swing.JLabel lblTotalTranAmt;
    private javax.swing.JLabel lblTotalVAT;
    private javax.swing.JLabel lblTranType;
    private javax.swing.JLabel lblVendorID;
    private javax.swing.JLabel lblVoucherDate;
    private javax.swing.JLabel lblVoucherNbr;
    private javax.swing.JPanel totalPanel;
    private javax.swing.JLabel totalTaxable;
    private javax.swing.JLabel totalTranAmt;
    private javax.swing.JLabel totalVAT;
    private javax.swing.JTextField txtDocStatus;
    private javax.swing.JTextField txtDueDate;
    private javax.swing.JTextField txtInvoice;
    private javax.swing.JTextField txtInvoiceDate;
    private javax.swing.JTextField txtOffSet;
    private javax.swing.JTextField txtVoucherDate;
    private javax.swing.JTextField txtVoucherNbr;
    // End of variables declaration//GEN-END:variables
}
